﻿using UnityEditor;
using UnityEngine;
using Util;
namespace UtilEditors
{
    public class SpriteRendererEditers : InspectorCom<SpriteRenderer>
    {
        public override void Init(SubsidiaryWindow win, int windownID)
        {
            base.Init(win, windownID);
            rect.width = 200;
            rect.height = 200;
        }
        public override void SelectionChange()
        {
            base.SelectionChange();
            rect.width = 200;
            rect.height = 200;
            int num = 0;
            float mw = 0;
            float mh = 0;
            for (int i = 0; num < component.Count; i++)
            {
                float cw = 0;
                float ch = 0;
                for (int j = 0; j < 5 && num < component.Count; j++)
                {
                    var _sr = component[num];
                    var t2 = AssetPreview.GetAssetPreview(_sr.sprite);
                    if (t2)
                    {
                        cw += t2.width;
                        num++;
                        if (t2.height > ch)
                        {
                            ch = t2.height;
                        }
                    }
                }

                if (cw > mw)
                {
                    mw = cw;
                }
                mh += ch;
            }
            rect.width = 200 + mw;
            rect.height = 200 + mh;

        }
        public override void OnIGUI()
        {
            if (Button("加层"))
            {
                component.byInvokeAll((_sr) =>
                {
                    Undo.RecordObject(_sr, "SpriteRenderer SortingOrder ++");
                    _sr.sortingOrder += 1;
                });
            }
            if (Button("减层"))
            {
                component.byInvokeAll((_sr) =>
                {
                    Undo.RecordObject(_sr, "SpriteRenderer SortingOrder --");
                    _sr.sortingOrder -= 1;
                });
            }
            int num = 0;
            StrY();
            for (int i = 0; num < component.Count; i++)
            {
                StrX();
                for (int j = 0; j < 5 && num < component.Count; j++)
                {
                    var _sr = component[num];
                    if (_sr)
                    {
                        var t2 = AssetPreview.GetAssetPreview(_sr.sprite);
                        GUILayout.Box(t2);
                        num++;
                    }
                }
                EndX();
            }
            EndY();


            Event e = Event.current;
            if (e.isKey)
            {
                if (e.keyCode == KeyCode.Keypad8)
                {//y+
                    component.byInvokeAll((m_trans) =>
                    {
                        m_trans.transform.position = m_trans.transform.position.Add(Vector2.up * 0.001f);
                    });
                }
                if (e.keyCode == KeyCode.Keypad5)
                {//y-
                    component.byInvokeAll((m_trans) =>
                    {
                        m_trans.transform.position = m_trans.transform.position.Add(Vector2.down * 0.001f);
                    });
                }
                if (e.keyCode == KeyCode.Keypad4)
                {//x-
                    component.byInvokeAll((m_trans) =>
                    {
                        m_trans.transform.position = m_trans.transform.position.Add(Vector2.left * 0.001f);
                    });
                }
                if (e.keyCode == KeyCode.Keypad6)
                {//x+
                    component.byInvokeAll((m_trans) =>
                    {
                        m_trans.transform.position = m_trans.transform.position.Add(Vector2.right * 0.001f);
                    });
                }
            }
        }
    }
}